{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 附录B 深度学习环境部署 \n",
    "\n",
    "____\n",
    "\n",
    "本书使用Keras、Caffe深度学习类库，其中Keras的安装依赖Tensorflow。对于一些系统环境，Caffe安装略复杂，新手可以先暂且跳过仅仅阅读本书Keras相关的部分，一定程度之后再选择Caffe作为进阶的工具。\n",
    "\n",
    "安装前提：请确认安装好附录A的python类库，如numpy、opencv等。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## B.1 部署Keras\n",
    "\n",
    "Keras支持TensorFlow和Theano作为后端引擎，推荐使用TensorFlow为后端。Keras的主要依赖包：\n",
    "\n",
    "- scipy 科学计算库\n",
    "- pyyaml\n",
    "- HDF5、h5py 可选，一种为存储和处理大容量科学数据设计的文件格式及相应库文件，仅在模型的save/load函数中使用\n",
    "\n",
    "这些使用conda（优先）或者pip命令安装即可，conda、pip使用见附录A。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### B.1.1 安装CUDA（非必须）\n",
    "\n",
    "可以安装CUDA支持GPU模式。\n",
    "\n",
    "CUDA Toolkit是NVIDIA公司面向GPU编程提供的基础工具包，也是驱动显卡计算的核心技术工具。从https://developer.nvidia.com/cuda-downloads 下载并安装CUDA,从http://www.nvidia.com/object/mac-driver-archive.html 下载并安装最新的CUDA独立驱动.\n",
    "\n",
    "在设置路径的时候注意对应版本号。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "export PATH=/Developer/NVIDIA/CUDA-7.5/bin:$PATH\n",
    "\n",
    "export DYLD_LIBRARY_PATH=/Developer/NVIDIA/CUDA-7.5/lib:$DYLD_LIBRARY_PATH"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### B.1.2 安装CuDNN（非必须）\n",
    "\n",
    "GPU模式下，Keras中使用CNN时可以考虑安装CuDNN加速。\n",
    "\n",
    "从https://developer.nvidia.com/cudnn 下载cuDNN库，解压出来是名为cuda的文件夹，里面有bin、include、lib。将三个文件夹复制到安装CUDA的地方，覆盖对应文件夹即可。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### B.1. 安装Tensorflow\n",
    "\n",
    "推荐基于conda安装，运行下面的命令"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "conda install tensorflow"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "也可以选择pip安装，或者从 https://github.com/tensorflow/tensorflow 官网下载源码手动安装。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### B.1.4 安装Keras\n",
    "\n",
    "从 https://github.com/fchollet/keras 下载Keras源码，cd到Keras的文件夹中，并运行下面的安装命令："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "python setup.py install"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "你也可以使用pip来安装Keras"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "pip install keras -U --pre"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## B.2 部署Caffe\n",
    "\n",
    "我们分为Windows系统用户和MacOS&Linux系统用户介绍Caffe环境的部署。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### B.2.1 Windows\n",
    "\n",
    "Windows用户需准备下面的工具库：\n",
    "\n",
    "- VS2013\n",
    "- CMake\n",
    "\n",
    "Caffe已经支持Windows，从 https://github.com/Microsoft/caffe/releases 直接下载双击安装即可。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### B.2.2 MacOS & Linux\n",
    "\n",
    "MacOS和Linux整体安装流程相似。\n",
    "\n",
    "#### 安装CUDA、CuDNN（非必须）\n",
    "\n",
    "GPU模式依赖的类库，见上文B.1.1、B.1.2。使CuDNN生效需要安装好Caffe之后，在Makefile.config文件中取消USE_CUDNN := 1的注释。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 安装BLAS-Intel MKL(仅针对Mac用户)\n",
    "\n",
    "BLAS是基础线性代数程序集，Mac系统自带的BLAS库存在不稳定问题，推荐OpenBLAS，运算矩阵更快，安装也简单。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "brew install openblas"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "也可以使用Intel MKL库。在校大学生可以使用学校邮箱在https://software.intel.com/en-us/qualify-for-free-software/student 申请Intel Parallel XE 2015安装包。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 安装依赖项\n",
    "\n",
    "Caffe的主要依赖包：\n",
    "\n",
    "- Boost C++库\n",
    "- leveldb\n",
    "- lmdb\n",
    "- GLog 日志库\n",
    "- GFlags 命令行参数库\n",
    "- Protobuff 一种数据标准库\n",
    "- HDF5 一种为存储和处理大容量科学数据设计的文件格式及相应库文件\n",
    "- Snappy 压缩程序库\n",
    "- OpenCV 图像处理库\n",
    "- szip 压缩库\n",
    "- boost-python C++和Python语言之间的交互库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "brew install -vd snappy leveldb lmdb gflags glog szip\n",
    "brew tap homebrew/science\n",
    "brew install wget hdf5 opencv protobuf boost boost-python"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "#### Makefile\n",
    "\n",
    "下载caffe源码，切换到caffe目录准备make配置文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "git clone https://github.com/bvlc/caffe.git\n",
    "cd caffe/\n",
    "cp Makefile.config.example Makefile.config"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用CPU模式打开注释"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "CPU_ONLY := 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "修改Makefile.config中关于BLAS的配置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# MKL库 BLAS:=MKL\n",
    "BLAS := open\n",
    "BLAS_INCLUDE := /usr/local/Cellar/openblas/0.2.19/include\n",
    "BLAS_LIB := /usr/local/Cellar/openblas/0.2.19/lib"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "根据你自己的Python环境修改剩余配置，包括运行命令和include，lib库等，Makefile.config中有推荐对应pip，brew，anaconda的配置。以annconda环境为例："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "ANACONDA_HOME := $(HOME)/anaconda2\n",
    "\n",
    "PYTHON_INCLUDE := $(ANACONDA_HOME)/include/python2.7 \\\n",
    "    $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include \\\n",
    "    \n",
    "PYTHON_LIB := $(ANACONDA_HOME)/lib"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "仔细过一遍其它配置选项，按个人情况修改，"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 编译"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "make clean\n",
    "make all"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "make runtest\n",
    "make pytest\n",
    "make pycaffe"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "测试程序运行过程中可能会发现缺失一些类库，如：pydot，graphviz，使用conda或者brew安装即可。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### PATH\n",
    "\n",
    "测试通过后，将编译好的Caffe目录放到环境变量中，"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "export PYTHONPATH=<你的Caffe安装目录>/python:$PYTHONPATH"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### B.2.4 补充\n",
    "\n",
    "在不同环境下反馈的Caffe安装问题较多，大部分集中编译环节，对于个别存在make兼容问题的系统，可以考虑安装cmake编译。如果问题还存在可以考虑牺牲性能，建立Docker等虚拟环境运行Caffe，具体请自行查阅相关资料，这里略过。"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
